# -*- coding: utf-8 -*-
import datetime
import sys
from time import sleep
import re
import requests
from selenium import webdriver
from selenium.common.exceptions import ElementClickInterceptedException, NoSuchElementException
from selenium.webdriver import DesiredCapabilities
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import json

sys.setrecursionlimit(1000000)
# chrome_options 初始化选项
chrome_options = webdriver.ChromeOptions()
# 设置浏览器初始 位置x,y & 宽高x,y
chrome_options.add_argument("--disable-blink-features=AutomationControlled")
chrome_options.add_argument(f'--window-position={217},{92}')
chrome_options.add_argument(f'--window-size={1222},{816}')
chrome_options.add_experimental_option("excludeSwitches", ['enable-automation'])
# chrome_options.add_experimental_option("prefs", {"profile.managed_default_content_settings.images": 2})
chrome_options.add_argument('–headless')
desired_capabilities = DesiredCapabilities.CHROME  # 修改页面加载策略
desired_capabilities["pageLoadStrategy"] = "none"
driver = webdriver.Chrome(options=chrome_options)

# with open('stealth.min.js') as f:
#     js = f.read()
# driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
#   "source": js
# })
driver.set_page_load_timeout(20)
driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
    "source": """Object.defineProperty(navigator, 'webdriver', {get: () => undefined})""",
})
wait = WebDriverWait(driver, 20)


def main():
    driver.get('https://passport.meituan.com/account/unitivelogin')
    driver.find_element_by_xpath("//input[@id='login-email']").send_keys('')
    sleep(2)
    driver.find_element_by_xpath("//input[@id='login-password']").send_keys('')
    save_cookies()
    get_cookies()
    driver.find_elements_by_xpath("//input[@class='btn']")[0].click()
    caozuo()
    sleep(3)
    driver.find_element_by_xpath("//button[@id='yodaSmsCodeBtn']").click()
    caozuo()
    driver.find_element_by_xpath("//button[@id='yodaSubmit']").click()


def save_cookies():
    cookies_dict = {}
    cookies = driver.get_cookies()
    for cookie in cookies:
        cookies_dict[cookie['name']] = cookie['value']

    with open("cookies.txt", 'w+') as file:
        json.dump(cookies_dict, file)


def get_cookies():
    with open('cookies.txt', 'r') as file:
        cookies = json.load(file)
    print(cookies)
    return cookies


def caozuo():
    sleep(3)
    hover = driver.find_element_by_xpath("//div[@id='yodaBox']")

    # 使用动作链（ActionChains）进行滑动
    action = webdriver.ActionChains(driver)
    # 点击需要操作的按钮,并且不释放
    # 后续有一系列的操作
    action.click_and_hold(hover).perform()
    # 移动 让移动慢一点
    action.move_by_offset(25, 0)
    sleep(0.1)
    action.move_by_offset(34, 0)
    sleep(0.1)
    action.move_by_offset(57, 0)
    sleep(0.1)
    action.move_by_offset(24, 0)
    sleep(0.1)
    action.move_by_offset(14, 0)
    sleep(0.1)
    action.move_by_offset(64, 0)
    # 释放
    action.release().perform()
    input('123')


if __name__ == '__main__':
    main()
